<?php

$this->freshSession();
if ($this->user['uid']) {
    $lock_key = 'tianyu_redis_lock_sendmail_' . $this->user['uid'];
    $ok = common_redis()->set($lock_key, 1, array('nx', 'ex' => 60));   //用redis控制用户过快访问的问题，这个OK只有不存在时才会创建，过期时间15秒，即时说，OK第一次访问是为true,接下来30秒内的访问都为false,除非把这个key删除。
    if ($ok) {
        $rechargeId = $this->getRechId();
        $uid = $this->user['uid'];
        $amount = floatval($_POST['amount']);
        $time = date('Y-m-d H:i:s', time());
        $username = $_POST['username'];
        $depositTime = $_POST['depositTime'];
        $cfgId = $_POST['cfgId'];
        if ($amount && $uid && $rechargeId && $depositTime && $username) {
            $para = array();
            $para['amount'] = $amount;
            $para['rechargeId'] = $rechargeId;
            $para['actionTime'] = $this->time;
            $para['uid'] = $this->user['uid'];
            $para['username'] = $this->user['username'];
            $para['actionIP'] = $this->ip(true);
            if ($cfgId == '288') {
                $para['info'] = '微信转账充值';
                $para['depositinfo'] = '微信昵称:' . $username . '<br>存款时间:' . $depositTime;
                $para['rechType'] = 'weixin';
            } elseif ($cfgId == '289') {
                $para['info'] = '农业银行';
                $para['depositinfo'] = '农业银行用户名:' . $username . '<br>存款时间:' . $depositTime;
                $para['rechType'] = 'cft';
            } elseif ($cfgId == '287') {
                $para['info'] = 'QQ转账';
                $para['depositinfo'] = 'QQ转账用户名:' . $username . '<br>存款时间:' . $depositTime;
                $para['rechType'] = 'cft';
            } elseif ($cfgId == '293') {
                $para['info'] = '农行转账';
                $para['depositinfo'] = '农行用户名:' . $username . '<br>存款时间:' . $depositTime;
                $para['rechType'] = 'cft';
            } elseif ($cfgId == '294') {
                $para['info'] = '建行转账';
                $para['depositinfo'] = '建行用户名:' . $username . '<br>存款时间:' . $depositTime;
                $para['rechType'] = 'cft';
            } else {
                $para['info'] = '支付宝转账';
                $para['depositinfo'] = '支付宝昵称:' . $username . '<br>存款时间:' . $depositTime;
                $para['rechType'] = 'alipay';
            }
            $para['code'] = random_char();
            if ($this->insertRow($this->prename . 'member_recharge', $para)) {
                //send_email($para);
                $queue = common_redis()->rPush('queue:tianyu_email',json_encode($para));
                //$taskid = Equeue::enterq(['name'=>'sendemail','data'=>$para]);//入列
                //common_redis()->del($lock_key); //删除锁
                echo '存款信息提交成功，请等待客服审核';
                
                exit;
            } else {
                common_redis()->del($lock_key); //删除锁
                echo '提交失败,请联系客服处理';
                exit;
            }
        } else {
            common_redis()->del($lock_key); //删除锁
            echo '提交失败';
            exit;
        }
    }else {
        echo '您的请求过于频繁，请等待 '.  common_redis()->ttl($lock_key).' 秒后再操作';
    }
}

function send_email($para) {
     global $glob_config;
    $done_url = $glob_config['hosts'].'/eorder/done.do?id='.$para['rechargeId'];
    $data_parms = [
        [
            'name' => '充值金额', 'values' => $para['amount']
        ],
        [
            'name' => '订单编号', 'values' => $para['rechargeId']
        ],
        [
            'name' => '用户名称', 'values' => $para['username']
        ],
        [
            'name' => '用户uid', 'values' => $para['uid']
        ],
        [
            'name' => '描述信息', 'values' => $para['depositinfo']
        ],
        [
            'name' => '类型', 'values' => $para['info']
        ],
        [
            'name' => '充值时间', 'values' => date('Y-m-d H:i:s', $para['actionTime'])
        ],
        [
            'name' => '操作IP', 'values' => long2ip($para['actionIP'])
        ],
        [
            'name' => '支付类型', 'values' => $para['rechType']
        ],
        [
            'name' => '操作安全码', 'values' => $para['code']
        ],
    ];

    $mail = new PHPMailer\PHPMailer\PHPMailer(true);                      // Passing `true` enables exceptions
    try {
        //Server settings
        $mail->SMTPDebug = 0;                                 // Enable verbose debug output
        $mail->isSMTP();                                      // Set mailer to use SMTP
        $mail->Host = 'smtp.163.com';  // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
        $mail->Username = 'mp5a6mg@163.com';                 // SMTP username
        $mail->Password = '310365asd_mp5@hh';                           // SMTP password
        //$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
        $mail->Port = 25;                                    // TCP port to connect to
        //Recipients
        $mail->setFrom('mp5a6mg@163.com', '天娱');
        $mail->addAddress('398541156@qq.com', '信号旗');     // Add a recipient
        //$mail->addAddress('ellen@example.com');               // Name is optional
//        $mail->addReplyTo('info@example.com', 'Information');
//        $mail->addCC('cc@example.com');
//        $mail->addBCC('bcc@example.com');
        //附件
        //$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
        //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
        //Content
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = '天娱支付审核通知';
        $mail->Body = htmlcreate($data_parms,$done_url);
        $mail->send();
        return true;
    } catch (Exception $e) {
        return $mail->ErrorInfo;
    }
}

function htmlcreate($data_parms,$done_url) {
    $lists = '';
    foreach ($data_parms as $key => $v) {
        $lists.='<tr> 
     <td align="center"> 
      <table width="600" border="0" cellspacing="0" cellpadding="0"> 
       <tbody>
        <tr> 
         <td align="center" style="border-bottom:1px solid #ecf0f1;"> 
          <table width="550" border="0" cellspacing="0" cellpadding="0"> 
           <tbody>
            <tr> 
             <td height="15"></td> 
            </tr> 
            <tr> 
             <td> 
              <table width="100%" border="0" cellspacing="0" cellpadding="0"> 
               <tbody>
                <tr> 
                 <td width="250" align="left" valign="top" style="font-family: \'Open Sans\', Arial, sans-serif; font-size:14px; color:#3b3b3b; line-height:26px;  font-weight: bold;">' . $v['name'] . '</td> 
                 <td width="300" align="left" valign="top" style="font-family: \'Open Sans\', Arial, sans-serif; font-size:14px; color:#3b3b3b; line-height:26px;  font-weight: bold;"><span>' . $v['values'] . '</span></td> 
                </tr> 
               </tbody>
              </table> </td> 
            </tr> 
            <tr> 
             <td height="5"></td> 
            </tr> 
           </tbody>
          </table> </td> 
        </tr> 
        <tr> 
         <td height="5"></td> 
        </tr> 
       </tbody>
      </table> </td> 
    </tr>';
    }


    $html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
 <head> 
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1" /> 
  <title>天娱收款支付系统</title> 
  <style type="text/css">
        .ReadMsgBody { width: 100%; background-color: #ffffff; }
        .ExternalClass { width: 100%; background-color: #ffffff; }
        .ExternalClass, .ExternalClass p, .ExternalClass span, .ExternalClass font, .ExternalClass td, .ExternalClass div { line-height: 100%; }
        html { width: 100%; }
        body { -webkit-text-size-adjust: none; -ms-text-size-adjust: none; margin: 0; padding: 0; }
        table { border-spacing: 0; border-collapse: collapse; table-layout: fixed; margin:0 auto; }
        table table table { table-layout: auto; }
        img { display: block !important; }
        table td { border-collapse: collapse; }
        .yshortcuts a { border-bottom: none !important; }
        a { color: #ff646a; text-decoration: none;}
        .textbutton a { font-family: "open sans", arial, sans-serif !important; color: #ffffff !important; }
        .footer-link a { color: #7f8c8d !important; }
    </style> 
 </head> 
 <body> 
  <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#ffffff"> 
   <!-- header --> 
   <tbody>
    <tr> 
     <td align="center"> 
      <table bgcolor="#f8f8f8" width="100%" border="0" align="center" cellpadding="0" cellspacing="0"> 
       <tbody>
        <tr align="center" valign="top"> 
         <td> 
          <table width="600" border="0" align="center" cellpadding="0" cellspacing="0"> 
           <tbody>
            <tr> 
             <td width="208" align="center" valign="top" bgcolor="#607e9d"> 
              <table width="158" border="0" align="center" cellpadding="0" cellspacing="0"> 
               <tbody>
                <tr> 
                 <td height="50"></td> 
                </tr> 
                <!-- Compane Name --> 
                <tr> 
                 <td style="font-family: \'Open Sans\', Arial, sans-serif; font-size:16px; color:#FFFFFF; line-height:26px; font-weight: bold;">天娱收款支付系统</td> 
                </tr> 
                <!-- end Compane Name --> 
                <tr> 
                 <td height="5"></td> 
                </tr> 
                <!-- address --> 
                <tr> 
                 <td style="font-family: \'Open Sans\', Arial, sans-serif; font-size:13px; color:#FFFFFF; line-height:26px;"> 天娱收款 <br /> 邮件通知 </td> 
                </tr> 
                <!-- end address --> 
                <tr> 
                 <td height="25"></td> 
                </tr> 
               </tbody>
              </table> </td> 
             <td width="392" align="center" valign="top"> 
              <table width="342" border="0" align="center" cellpadding="0" cellspacing="0"> 
               <tbody>
                <tr> 
                 <td height="50"></td> 
                </tr> 
                <!-- title --> 
                <tr> 
                 <td align="right" style="font-family: \'Open Sans\', Arial, sans-serif; font-size:38px; color:#3b3b3b; line-height:26px;">支付审核通知</td> 
                </tr> 
                <!-- end title --> 
                <tr> 
                 <td height="25"></td> 
                </tr> 
                <tr> 
                 <td align="right"> 
                  <table align="right" width="50" border="0" cellpadding="0" cellspacing="0"> 
                   <tbody>
                    <tr> 
                     <td bgcolor="#ff646a" height="3" style="line-height:0px; font-size:0px;">&nbsp;</td> 
                    </tr> 
                   </tbody>
                  </table> </td> 
                </tr> 
                <tr> 
                 <td height="15"></td> 
                </tr> 
                <!-- Compane Name --> 
                <tr> 
                 <td align="right" style="font-family: \'Open Sans\', Arial, sans-serif; font-size:16px; color:#3b3b3b; line-height:26px; font-weight: bold;">Exrick Presents</td> 
                </tr> 
                <!-- end Compane Name --> 
                <tr> 
                 <td height="5"></td> 
                </tr> 
               </tbody>
              </table> </td> 
            </tr> 
           </tbody>
          </table> </td> 
        </tr> 
       </tbody>
      </table> </td> 
    </tr> 
    <!-- end header --> 
    <!-- title --> 
    <tr> 
     <td align="center"> 
      <table align="center" width="600" border="0" cellspacing="0" cellpadding="0"> 
       <tbody>
        <tr> 
         <td align="center" style="border-bottom:3px solid #bcbcbc;"> 
          <table align="center" width="550" border="0" cellspacing="0" cellpadding="0"> 
           <tbody>
            <tr> 
             <td height="20"></td> 
            </tr> 
            <!-- header --> 
            <tr> 
             <td> 
              <table width="100%" border="0" cellspacing="0" cellpadding="0"> 
               <tbody>
                <tr> 
                 <td width="100%" align="left" valign="top" style="font-size:13px; color:#3b3b3b; line-height:26px; text-transform:uppercase;"> 尊敬的管理员您好，您收到了来自<span th:text="天娱支付"></span>订单，请您及时处理： </td> 
                </tr> 
               </tbody>
              </table> </td> 
            </tr> 
            <!-- end header --> 
            <tr> 
             <td height="10"></td> 
            </tr> 
           </tbody>
          </table> </td> 
        </tr> 
       </tbody>
      </table> </td> 
    </tr> 
    <!-- end title --> 
    <!-- list --> 
    ' . $lists . ' 
    <!-- end list --> 
    <!-- total --> 
    <tr> 
     <td align="center"> 
      <table align="center" width="600" border="0" cellspacing="0" cellpadding="0"> 
       <tbody>
        <tr> 
         <td align="center" height="0" style="border-bottom:3px solid #3b3b3b;"></td> 
        </tr> 
       </tbody>
      </table> </td> 
    </tr> 
    <!-- end total --> 
    <!-- note --> 
    <tr> 
     <td align="center"> 
      <table align="center" width="600" border="0" cellspacing="0" cellpadding="0"> 
       <tbody>
        <tr> 
         <td height="20"></td> 
        </tr> 
        <!-- content --> 
        <tr> 
         <td style="font-size:13px; color:#7f8c8d; line-height:26px;"> 请确认后点击以下对应按钮进行审核： </td> 
        </tr> 
        <!-- end content --> 
        <tr> 
         <td height="20"></td> 
        </tr> 
        <tr> 
         <td align="center"> 
          <!--button--> 
          <table width="80%" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#91c444" class="textbutton" style="border-radius:5px;border-bottom:3px solid #e6e6e6"> 
           <tbody>
            <tr> 
             <td height="40" align="center" style="font-size:16px;color:#FFFFFF;line-height: 28px;padding-left: 15px;padding-right: 15px;"> <a href="'.$done_url.'" style="display: block;">审核通过</a> </td> 
            </tr> 
           </tbody>
          </table> 
          <!--end button--> </td> 
        </tr> 
        <tr> 
         <td height="15"></td> 
        </tr> 
        <tr> 
         <td align="center"> 
          <!--button--> 
          <table width="80%" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#e4ba00" class="textbutton" style="border-radius:5px;border-bottom:3px solid #e6e6e6"> 
           <tbody>
            <tr> 
             <td height="40" align="center" style="font-size:16px;color:#FFFFFF;line-height: 28px;padding-left: 15px;padding-right: 15px;"> <a href="'.$done_url.'&del=1"  style="display: block;">驳回</a> </td> 
            </tr> 
           </tbody>
          </table> 
          <!--end button--> </td> 
        </tr> 
        <tr> 
         <td height="15" style="border-bottom:3px solid #bcbcbc;"></td> 
        </tr> 
       </tbody>
      </table> </td> 
    </tr> 
    <!-- end note --> 
   </tbody>
  </table>  
 </body>
</html>';
    return $html;
}
